package com.boot.security.server.controller.qk;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.boot.security.server.model.JhSysUser;

/**
 *	救火商城后台登录入口 
 * @author Linxx
 *
 */

@RestController
@RequestMapping("/qk_login")
public class QKLoginController {
	
	private static final Logger log = LoggerFactory.getLogger("adminLogger");
	
	@PostMapping("/login")
	public String login(String acc,String pwd,HttpSession session) {
		if(acc.equals("admin")&&pwd.equals("123")) {
			JhSysUser user = new JhSysUser(acc, pwd);
			session.setAttribute("user",user);
			JSONObject json =  new JSONObject(true);
			json.put("state", "success");
			return json.toString();
		}else {
			JSONObject json =  new JSONObject(true);
			json.put("state", "failed");
			return json.toString();
		}
	}
	
	@GetMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		JSONObject json = new JSONObject(true);
		json.put("state", "success");
		return json.toString();
	}
	
	@GetMapping("/haveSession")
	public String havesession(HttpSession session) {
		JSONObject json = new JSONObject(true);
		if(null!=session.getAttribute("user")) {
			json.put("state","yes");
		}else {
			json.put("state","no");
		}
		return json.toString();
	}
	
	@GetMapping("/removeSession")
	public String removesession(HttpSession session) {
		session.removeAttribute("user");
		JSONObject json = new JSONObject(true);
		json.put("state","yes");
		return json.toString();
	}
}
